﻿using System;
using QuadraticEquation;

namespace QuadraticEquationPresenter
{
    public class Presenter
    {
        private IQuadraticEquationView view;
        public ILogger logger;
        public IFactoryLogger factoryLogger;

        private Presenter() { }
        public Presenter(IQuadraticEquationView view, IFactoryLogger factoryLogger)
        {
            this.view = view;
            this.factoryLogger = factoryLogger;
            this.view.solveButtonPressed += solveButtonPressed;
            this.view.showLogButtonPressed += showLogButtonPressed;
            this.view.loggerStateChangedToFileLogger += loggerStateChangedToFileLogger;
            logger = factoryLogger.CreateFileLogger();
        }

        private void showLogButtonPressed()
        {
            logger.write("showLogButtonPressed");
            view.logText = logger.read();
        }

        private void solveButtonPressed()
        {
            logger.write("solveButtonPressed");
            view.errorMessageField = "";
            try
            {
                this.solveEquation();
            }
            catch (Exception ex)
            {
                view.errorMessageField = "incorrect data";
                return;
            }
        }

        private void loggerStateChangedToFileLogger(bool file)
        {
            if (file) logger = factoryLogger.CreateFileLogger();
            else logger = factoryLogger.CreateHTMLLogger();
        }

        private void solveEquation()
        {
            SolvingQuadraticEquation equationSolver = new SolvingQuadraticEquation();
            if (System.Convert.ToDouble(view.aField) == 0)
            {
                view.errorMessageField = "a should not be 0";
                view.x1Field = "";
                view.x2Field = "";
                view.messageAboutRootsField = "";
                return;
            }
            SolutionEquation solutionEquation = equationSolver.solve(System.Convert.ToDouble(view.aField), System.Convert.ToDouble(view.bField), System.Convert.ToDouble(view.cField));
            if (solutionEquation.GetCount() == 2)
            {
                view.x1Field = System.Convert.ToString(solutionEquation.GetX1());
                view.x2Field = System.Convert.ToString(solutionEquation.GetX2());
                view.messageAboutRootsField = "two different roots of the equation";
            }
            if (solutionEquation.GetCount() == 1)
            {
                view.x1Field = System.Convert.ToString(solutionEquation.GetX1());
                view.x2Field = System.Convert.ToString(solutionEquation.GetX2());
                view.messageAboutRootsField = "two of the same root of the equation";
            }
            if (solutionEquation.GetCount() == 0)
            {
                view.x1Field = "";
                view.x2Field = "";
                view.messageAboutRootsField = "equation has no real roots";
            }
        }
    }
}
